package com.hzqy.service.industry.medical;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hzqy.persistence.dao.industry.IndustryMedicalDao;
import com.hzqy.service.core.GenericServiceBean;
import com.hzqy.web.vo.industry.medical.ItvConsumerMedicalVo;
import com.hzqy.web.vo.industry.medical.ItvMedicalDeviceVo;
@Service
public class IndustryMedicalServiceBean  extends GenericServiceBean<ItvConsumerMedicalVo, IndustryMedicalDao, Integer>{
	private static final long serialVersionUID = 1L;
	protected final Logger LOG = LoggerFactory.getLogger(getClass());
	@Resource
	private IndustryMedicalDao industryMedicalDao;
	@Override
	public IndustryMedicalDao getEntityDao() {
		return industryMedicalDao;
	}
	public Map<String, Object> postConsumer(JSONObject request) { 
		LOG.debug(request.toJSONString());
		Map<String,Object> resultMap = new HashMap<String, Object>();
    	String mobile=(String) request.get("mobile");
    	List<String> errorItvAccount=new ArrayList<String>();
    	List<String> errorDevice=new ArrayList<String>();
    	int flag=0;
    	if(mobile!=null&&mobile.length()>0){
    		try{
    			JSONArray itList= request.getJSONArray("itvList");
        	if(itList!=null&&itList.size()>0){//处理业务账号绑定关系
        		for(int i=0;i<itList.size();i++){
        			JSONObject obj=JSON.parseObject(itList.get(i).toString()) ;
        			ItvConsumerMedicalVo itvConsumerMedicalVo=new ItvConsumerMedicalVo();
        			itvConsumerMedicalVo.setF_icm_itvAccount(obj.get("itvAccount")==null?"":obj.get("itvAccount").toString());
        			itvConsumerMedicalVo.setF_icm_mac(obj.get("mac")==null?"":obj.get("mac").toString());
        			itvConsumerMedicalVo.setF_icm_stbId(obj.get("stbId")==null?"":obj.get("stbId").toString());
        			itvConsumerMedicalVo.setF_icm_mobile(mobile);
        			industryMedicalDao.insertIndustryMedicalVo(itvConsumerMedicalVo);
        			if(itvConsumerMedicalVo.getF_icm_id()==0){//添加失败
        				errorItvAccount.add(String.valueOf(obj.get("itvAccount")));
        			}
        		}
        	}else{
        		flag=-1;
        	}
        	//
        	JSONArray deviceList=request.getJSONArray("deviceList");
        	if(deviceList!=null&&flag==0){
        		for(int i=0;i<deviceList.size();i++){
        			JSONObject obj=JSON.parseObject(deviceList.get(i).toString()) ;
        			ItvMedicalDeviceVo itvMedicalDeviceVo=new ItvMedicalDeviceVo();
        			itvMedicalDeviceVo.setF_imd_deviceName(obj.get("deviceName")==null?"":obj.get("deviceName").toString());
        			itvMedicalDeviceVo.setF_imd_deviceId(obj.get("device")==null?"":obj.get("device").toString());
        			itvMedicalDeviceVo.setF_imd_ext(obj.get("ext")==null?"":obj.get("ext").toString());
        			itvMedicalDeviceVo.setF_imd_notifyType(obj.get("notifyType")==null?"":obj.get("notifyType").toString());
        			itvMedicalDeviceVo.setF_imd_mobile(mobile);
        			industryMedicalDao.insertItvMedicalDeviceVo(itvMedicalDeviceVo);
        			if(itvMedicalDeviceVo.getF_imd_id()==0){//添加失败
        				errorDevice.add(String.valueOf(obj.get("device")));
        			}
        		}
        	}
        	if(errorDevice.size()>0){
        		LOG.error("舟山智慧养老数据获取错误："+errorDevice.toString()+"deviceListStr=="+String.valueOf(deviceList));
        	}
        	if(errorItvAccount.size()>0){
        		LOG.error("舟山智慧养老数据获取错误："+String.valueOf(deviceList)+"deviceListStr=="+errorItvAccount);
        	}
        	
        	resultMap.put("code", 0);
    		resultMap.put("desc", "处理完成");
    		resultMap.put("data", "");
    		}catch(Exception e){
    			LOG.error("数据入库时错误",e);
    			resultMap.put("code", -1);
        		resultMap.put("desc", "绑定用户同步失败,数据入库时错误");
        		resultMap.put("data", "");
    		}
    	}else{
    		resultMap.put("code", -1);
    		resultMap.put("desc", "绑定用户同步失败,无法识别用户");
    		resultMap.put("data", "");
    	}
		return resultMap;
	}

	
	
}